(* Dimensions d'une image *)
let get_dims img =
  ((Sdlvideo.surface_info img).Sdlvideo.w, (Sdlvideo.surface_info img).Sdlvideo.h)
 
(* init de SDL *)
let sdl_init () =
  begin
    Sdl.init [`EVERYTHING];
    Sdlevent.enable_events Sdlevent.all_events_mask;
  end
 
(* attendre une touche ... *)
let rec wait_key () =
  let e = Sdlevent.wait_event () in
    match e with
    Sdlevent.KEYDOWN _ -> ()
      | _ -> wait_key ()
 
(*
  show img dst
  affiche la surface img sur la surface de destination dst (normalement l'écran)
*)
let show img dst =
  let d = Sdlvideo.display_format img in
    Sdlvideo.blit_surface d dst ();
    Sdlvideo.flip dst

let filtreRouge img x y =
  ((x<>0 && y<>0 && x<>fst(get_dims img) && y<>snd(get_dims img)) &&
     ((Sdlvideo.get_pixel_color img (x-1) (y-1))<> (255,0,255))&&
     ((Sdlvideo.get_pixel_color img (x-1) (y))<> (255,0,255))&&
     ((Sdlvideo.get_pixel_color img (x-1) (y+1))<> (255,0,255))&&
     ((Sdlvideo.get_pixel_color img (x) (y-1))<> (255,0,255))&&
     ((Sdlvideo.get_pixel_color img (x) (y))<> (255,0,255))&&
     ((Sdlvideo.get_pixel_color img (x) (y+1))<> (255,0,255))&&
     ((Sdlvideo.get_pixel_color img (x+1) (y-1))<> (255,0,255))&&
     ((Sdlvideo.get_pixel_color img (x+1) (y))<> (255,0,255))&&
     ((Sdlvideo.get_pixel_color img (x+1) (y+1))<> (255,0,255)))


(*repere collision avec diagonale et contour*)
let diago img i j = 
  begin
    let x = ref i in
    let y = ref j in 
      while !y <= snd(get_dims img)-1 && !x <= fst(get_dims img)-1 do
	if ((Sdlvideo.get_pixel_color img !x !y = (0,0,0)) && (filtreRouge img !x !y)) then
	  Sdlvideo.put_pixel_color img !x !y (255,0,255)
	else
	  Sdlvideo.put_pixel_color img !x !y (0,0,0);
	y := !y +1;
	x := !x +1;
      done
  end

let diagonal img inter = 
  begin
    let j = ref inter in
    let i = ref 0 in 
      while !j <= snd(get_dims img)-1 do	
	diago img 0 !j;
	j := !j + inter
      done;
      while !i <= fst(get_dims img)-1 do
	diago img !i 0; 
	i := !i + inter
      done
  end

(*vertical et horizon dernier trait non géré*)
(*traite les collisions horizontales et verticales avec les contours*)
let horivert img inter = 
  begin
    let j = ref 0 and i = ref 0 in 
      while !j <= snd(get_dims img)-1 do  	
	for  i = 0 to fst(get_dims img)-1 do         
	  if ((Sdlvideo.get_pixel_color img i !j = (0,0,0)) && ((!j mod inter) = 0) && (filtreRouge img i !j)) then 
	    Sdlvideo.put_pixel_color img i !j (255,0,255)
	  else
	    Sdlvideo.put_pixel_color img i !j (0,0,0);
    
	done;
	j := !j + inter
      done;
      while !i <= fst(get_dims img)-1 do
	for j = 0 to snd(get_dims img)-1 do 	  
	  if ((Sdlvideo.get_pixel_color img !i j = (0,0,0)) && ((!i mod inter) = 0) && (filtreRouge img !i j)) then 
	    Sdlvideo.put_pixel_color img !i j (255,0,255)
	  else
	    Sdlvideo.put_pixel_color img !i j (0,0,0);     
	done;
	i := !i + inter
      done;     
  end

let listedepoints img =
  begin
    let l = ref [] in
    for i = 0 to fst(get_dims img)-1 do 
      for  j = 0 to snd(get_dims img)-1 do
	if ((Sdlvideo.get_pixel_color img i j = (255,0,255))) then 
	  l := (i,j)::!l;
      done;
    done;
    !l;
  end


(* main *)
let main src dst interval quadrillage_ou_point_obj ()=
  begin
    (* Nous voulons 1 argument *)
    (*if Array.length (Sys.argv) < 2 then
      failwith "Il manque le nom du fichier!";*)
    (* Initialisation de SDL *)
    sdl_init ();
    (* Chargement d'une image *)
    (*let img = Sdlloader.load_image Sys.argv.(1) in*)
    let img = Sdlloader.load_image src in
    (* On récupère les dimensions *)
    (*let (w,h) = get_dims img in*)
    (* On crée la surface d'affichage en doublebuffering *)
    (*let display = Sdlvideo.set_video_mode w h [`DOUBLEBUF] in*)
      (*print_string("Entrer l'intervalle desiree:\n");*)
      (*let i = read_int() in*)
      let i = interval in
      (* on affiche l'image *)
      (*show img display;*)
      diagonal img i;
      horivert img i;
     
      (*wait_key();*)
      (*show img display;
      wait_key();
       
      wait_key();*)
      (*show img display;*)
     
      (*Sdlvideo.save_BMP img "tof.bmp";*)
      Sdlvideo.save_BMP img dst;
      listedepoints img;
  end
